# coding:utf-8
from numpy.ma.core import size
from mathlib import wavread
from OPAP.gha import Gha
from easygui import fileopenbox
import pylab
import sys

if __name__ == "__main__":
    
    wavFile = fileopenbox(title=u"ファイル選択", default="*.wav")
    xn, fs, bits = wavread(wavFile, False)

    #信号のパラメータを確認
    print u'信号長:', size(xn, 0)
    print u'サンプリング周波数:', fs, '[Hz]'
    print u'量子化ビット数', bits
    
    if size(xn, 1) == 2:
        xn = xn.flatten()[:xn.size / 2] #1チャンネル分だけ取り出す
    else:
        xn = xn.flatten()

    #ghaを実行
    try:
        jmax = int(raw_input('iteration >>> '))
    except:
        print 'number is required'
        sys.exit()
    freq, amp, phase = Gha(xn, fs).calc(jmax)
    
    pylab.stem(freq, amp)
    pylab.ylabel('amplitude[dB]')
    pylab.xlabel('frequency[Hz]')
    pylab.grid(True)
    pylab.show()
    